home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / MPW / MPW rman 1.3.4 / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-08  |  1.6 KB  |  80 lines  |  [TEXT/KAHL]

  1. /*
  2. Newsgroups: mod.std.unix
  3. Subject: public domain AT&T getopt source
  4. Date: 3 Nov 85 19:34:15 GMT
  5.  
  6. Here's something you've all been waiting for:  the AT&T public domain
  7. source for getopt(3).  It is the code which was given out at the 1985
  8. UNIFORUM conference in Dallas.  I obtained it by electronic mail
  9. directly from AT&T.  The people there assure me that it is indeed
  10. in the public domain.
  11. */
  12.  
  13. /*LINTLIBRARY*/
  14.  
  15. extern int strlen();
  16. extern int strcmp();
  17. extern char *strchr();
  18. extern int write();
  19.  
  20. #define NULL    0
  21. #define EOF    (-1)
  22. #define ERR(s, c)    if(opterr){\
  23.     char errbuf[2];\
  24.     errbuf[0] = c; errbuf[1] = '\n';\
  25.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  26.     (void) write(2, s, (unsigned)strlen(s));\
  27.     (void) write(2, errbuf, 2);}
  28.  
  29.  
  30. int    opterr = 1;
  31. int    optind = 1;
  32. int    optopt;
  33. char    *optarg;
  34.  
  35. int
  36. getopt(argc, argv, opts)
  37. int    argc;
  38. char    **argv, *opts;
  39. {
  40.     static int sp = 1;
  41.     register int c;
  42.     register char *cp;
  43.  
  44.     if(sp == 1)
  45.         if(optind >= argc ||
  46.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  47.             return(EOF);
  48.         else if(strcmp(argv[optind], "--") == NULL) {
  49.             optind++;
  50.             return(EOF);
  51.         }
  52.     optopt = c = argv[optind][sp];
  53.     if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  54.         ERR(": illegal option -- ", c);
  55.         if(argv[optind][++sp] == '\0') {
  56.             optind++;
  57.             sp = 1;
  58.         }
  59.         return('?');
  60.     }
  61.     if(*++cp == ':') {
  62.         if(argv[optind][sp+1] != '\0')
  63.             optarg = &argv[optind++][sp+1];
  64.         else if(++optind >= argc) {
  65.             ERR(": option requires an argument -- ", c);
  66.             sp = 1;
  67.             return('?');
  68.         } else
  69.             optarg = argv[optind++];
  70.         sp = 1;
  71.     } else {
  72.         if(argv[optind][++sp] == '\0') {
  73.             sp = 1;
  74.             optind++;
  75.         }
  76.         optarg = NULL;
  77.     }
  78.     return(c);
  79. }
  80.